/**  
* @Title: CrShopsService.java
* @Package com.lemon.service.cityRun
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年10月20日
* @version V1.0  
*/
package com.lemon.service.cityRun;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.lemon.conf.content.IdTypeHandler;
import com.lemon.mapper.cityRun.CrShopActivityOutMapper;
import com.lemon.mapper.cityRun.CrShopPrinterMapper;
import com.lemon.mapper.cityRun.CrShopSetMapper;
import com.lemon.mapper.cityRun.CrShopUserMapper;
import com.lemon.mapper.cityRun.CrShopsMapper;
import com.lemon.model.cityRun.CrShopActivityOut;
import com.lemon.model.cityRun.CrShopPrinter;
import com.lemon.model.cityRun.CrShopSet;
import com.lemon.model.cityRun.CrShopUser;
import com.lemon.model.cityRun.CrShops;
import com.lemon.utils.StringUtil;

/**
 * <p>Title: CrShopsService</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年10月20日
 * @版本 V 1.0 
 */
@Service
public class CrShopsServiceImpl implements CrShopsService {

	@Resource
	private CrShopsMapper shopMapper;
	@Resource
	private CrShopUserMapper uMapper;

	@Resource
	private CrShopSetMapper sMapper;
	@Resource
	private CrShopPrinterMapper pMapper;

	@Resource
	private CrShopActivityOutMapper actMapper;
	/**
	 * <p>
	 * Title: selectListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param shop
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月20日
	 */
	@Override
	public List<CrShops> selectListBySelective(CrShops shop) throws Exception {
		PageHelper.startPage(shop.getPageNum() , shop.getPageSize());
		List<CrShops> list = shopMapper.selectListBySelective(shop);
		return list;
	}

	/**
	 * <p>
	 * Title: selectShopById
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param shop
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月20日
	 */
	@Override
	public CrShops selectShopById(String id) throws Exception {
		CrShops shop = shopMapper.selectByPrimaryKey(id);
		return shop;
	}

	/**
	 * <p>
	 * Title: updateShopInfo
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param shop
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月20日
	 */
	@Override
	public int updateShopInfo(CrShops shop) throws Exception {
		int i = shopMapper.updateByPrimaryKeySelective(shop);
		return i;
	}

	/**
	 * <p>
	 * Title: insertShopsAndSUserInfo
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param u
	 * @param s
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年10月21日
	 */
	@Transactional(rollbackFor = { Exception.class } , propagation = Propagation.REQUIRED)
	@Override
	public int insertShopsAndSUserInfo(CrShopUser u , CrShops s) throws Exception {
		uMapper.insertSelective(u);
		s.setShopUserId(IdTypeHandler.encode(Long.parseLong(u.getId())));

		int i = shopMapper.insertSelective(s);
		return i;
	}

	/**
	 * <p>
	 * Title: selectShopAndUserById
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param id
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月21日
	 */
	@Override
	public CrShops selectShopAndUserBySel(CrShops css) throws Exception {

		CrShops cs = shopMapper.selectShopAndUserById(css);
		return cs;
	}

	/**
	 * <p>
	 * Title: updateShopAndUserInfo
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param shop
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月22日
	 */
	@Transactional(rollbackFor = { Exception.class } , propagation = Propagation.REQUIRED)
	@Override
	public int updateShopAndUserInfo(CrShops shop,CrShopUser user) throws Exception {
		if (StringUtil.isEmpty(user.getId())) {
			CrShops crShops = shopMapper.selectByPrimaryKey(shop.getId());
			user.setId(crShops.getShopUserId());
		}
		//修改用户资料
		int i = uMapper.updateByPrimaryKeySelective(user);
		//修改店铺资料
		int j = shopMapper.updateByPrimaryKeySelective(shop);
		return j;
	}

	/**
	 * <p>
	 * Title: selectAuditListBySelective 查询待审核的列表
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param shop
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月22日
	 */
	@Override
	public List<CrShops> selectAuditListBySelective(CrShops shop) throws Exception {
		PageHelper.startPage(shop.getPageNum() , shop.getPageSize());
		List<CrShops> list = shopMapper.selectAuditListBySelective(shop);
		return list;
	}

	/**
	 * <p>
	 * Title: selectShopSetByShopId
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param shop
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月1日
	 */
	@Override
	public CrShopSet selectShopSetByShopId(CrShopSet shop) throws Exception {
		return sMapper.selectBySelective(shop);
	}

	/**
	 * <p>
	 * Title: updateShopSetByShopId
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param shop
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月1日
	 */
	@Override
	public int updateShopSetByShopId(CrShopSet shop) throws Exception {
		return sMapper.updateByPrimaryKeySelective(shop);
	}

	/**
	 * <p>
	 * Title: insertShopSetInfo
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param shop
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月1日
	 */
	@Override
	public void insertShopSetInfo(CrShopSet shop) throws Exception {
		sMapper.insertSelective(shop);
	}

	/**
	 * <p>
	 * Title: selectPrintListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param printer
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月3日
	 */
	@Override
	public List<CrShopPrinter> selectPrintListBySelective(CrShopPrinter printer) throws Exception {
		PageHelper.startPage(printer.getPageNum() , printer.getPageSize());
		return pMapper.selectListBySelective(printer);
	}

	/**
	 * <p>
	 * Title: insertPrintSelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param printer
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月3日
	 */
	@Override
	public int insertPrintSelective(CrShopPrinter printer) throws Exception {
		return pMapper.insertSelective(printer);
	}

	/**
	 * <p>
	 * Title: updatePrintSelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param printer
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月3日
	 */
	@Override
	public int updatePrintSelective(CrShopPrinter printer) throws Exception {

		return pMapper.updateByPrimaryKeySelective(printer);
	}

	/**
	 * <p>
	 * Title: getDataStatisInfo
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月5日
	 */
	@Override
	public Map<String , Object> getDataStatisInfo(CrShops crShops) throws Exception {

		return shopMapper.getDataStatisInfo(crShops);
	}

	/**
	 * <p>
	 * Title: getShopOrderProfitInfo
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param crShops
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月5日
	 */
	@Override
	public Map<String , Object> getShopOrderProfitInfo(CrShops crShops) throws Exception {
		return shopMapper.getShopOrderProfitInfo(crShops);
	}

	/**
	 * <p>
	 * Title: selectxListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param shop
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月7日
	 */
	@Override
	public List<CrShops> selectxListBySelective(CrShops shop) throws Exception {
		PageHelper.startPage(shop.getPageNum() , shop.getPageSize());
		List<CrShops> list = shopMapper.selectShopListByLoc(shop);
		for (int i = 0; i < list.size(); i++) {
			CrShops shops = list.get(i);
			if (StringUtil.isNotBlank(shops.getShopSendPrice())) {
				String shopSendPrice = shops.getShopSendPrice();
				if ("0".equals(shopSendPrice)) {
					break;
				}
				String[] splits = shopSendPrice.split(",");
				for (int j = 0; j < splits.length; j++) {
					String[] split = splits[j].split("-");
					int shopDis = shops.getDistance();

					if (shopDis > 1000 * Double.parseDouble(split[0])
							&& shopDis <= 1000 * Double.parseDouble(split[1])) {
						list.get(i).setShopSendPrice(split[2]);
						break;
					}
				}
			}
			String id = list.get(i).getId();
			CrShopActivityOut out = new CrShopActivityOut();
			out.setWhere(" and a.status!=0 and a.status!=2 ");
			out.setShopId(id);
			List<CrShopActivityOut> list2 = actMapper.selectListBySelective(out);
			list.get(i).setAtList(list2);
		}
		return list;
	}

	/**
	 * <p>
	 * Title: selectShopAndAtvByPrimaryKey
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param id
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月9日
	 */
	@Override
	public CrShops selectShopAndAtvByPrimaryKey(String id) throws Exception {

		return shopMapper.selectShopAndAtvByPrimaryKey(id);
	}

	public static void main(String[] args) {

		String s = "2.5";
		double dd = Double.parseDouble(s);

		System.out.println(325 > 0 && 325 <= dd * 1000);
	}
}
